package com.yanze.schoolinformation.viewModule.login;

import com.yanze.schoolinformation.app.C;
import com.yanze.schoolinformation.model.bean.resp.RespUser;
import com.yanze.schoolinformation.model.http.DeforObserver;
import com.yanze.schoolinformation.model.http.reqo.DataReqo;
import com.yanze.schoolinformation.utils.BaseTools;
import com.yanze.schoolinformation.view.login_register.login.LoginFragment;

import io.reactivex.disposables.Disposable;

/**
 * Created by MrZheng on 2018/5/7.
 */

public class LoginViewModel {
    private LoginNavigator mNavigator;

    private LoginFragment mFragment;

    public LoginViewModel(LoginFragment mFragment) {
        this.mFragment = mFragment;
    }

    public void setmNavigator(LoginNavigator mNavigator) {
        this.mNavigator = mNavigator;
    }

    public void Login(String phone, String password) {
        DataReqo.getInstant().login(C.url.API_School, phone, BaseTools.MD5(password))
                .subscribe(new DeforObserver<RespUser>() {
                    @Override
                    public void _onSubscribe(Disposable disposable) {
                        mFragment.addDisposable(disposable);
                    }

                    @Override
                    public void _onNext(RespUser entity) {
                        if (entity.getStatus() == 200) {
                            mNavigator.registerSuccess(entity);
                        }else{
                            mNavigator.registerFaild(entity.getMsg());
                        }
                    }

                    @Override
                    public void _onError(String msg) {
                        mNavigator.registerFaild(msg);
                    }
                });
    }

    public void onDestory() {
        this.mFragment = null;
        this.mNavigator = null;
    }
}
